package hasthi.tools.analyze.suite;

import hasthi.common.HasthiException;
import hasthi.common.XmlBeanUtils;
import hasthi.test.ClientSetDocument;
import hasthi.test.FooDocument;
import hasthi.test.FooResponseDocument;
import hasthi.tools.analyze.ClientSet;
import hasthi.tools.analyze.PerfClient;

import org.apache.xmlbeans.XmlObject;

public class EchoClientSet implements PerfTask{
    private ClientSetDocument  clientSetData;
    public void init(XmlObject object) {
       this.clientSetData = (ClientSetDocument)object;
    }
    public void run() {
        try {
            System.out.println("Starting test thread");
            final FooDocument fooRequest = FooDocument.Factory.newInstance();
            fooRequest.addNewFoo().setIn("Hello");
            hasthi.test.ClientSetDocument.ClientSet clientSetDatatype = clientSetData.getClientSet();
            PerfClient.PerfResponseVerifier perfResponseVerifier = new PerfClient.PerfResponseVerifier() {
                public boolean verify(XmlObject response) {
                    String in = ((FooDocument)fooRequest).getFoo().getIn();
                    if(response instanceof FooResponseDocument){
                        String out = ((FooResponseDocument)response).getFooResponse().getOut();
                        return in.equals(out);
                    }else{
                        System.out.println(XmlBeanUtils.prettyPrint(response));
                        return false;
                    }
                }
            };
            ClientSet clientSet = new ClientSet(clientSetDatatype.getTestName(),clientSetDatatype.getClientCount(),
                    clientSetDatatype.getRequestCountPerClient(),fooRequest,clientSetDatatype.getServiceUrl(),perfResponseVerifier);
            clientSet.startWSDMClient();
            clientSet.setEndTime(System.currentTimeMillis() +clientSetDatatype.getTestTimePeriod());
            System.out.println("Starting test suite with "+ clientSetDatatype.getClientCount() + " clients");
            clientSet.runTests();
            System.out.println("Finsied test suite with "+ clientSetDatatype.getClientCount() + " clients");
        } catch (HasthiException e) {
            System.out.println("error Occured");
            e.printStackTrace();
        }
    }
}
